/**
 * 
 */
package com.williamoneil.charts.client.controller;

import java.util.Date;
import java.util.HashMap;
import java.util.Map;

import com.google.gwt.user.client.rpc.AsyncCallback;
import com.williamoneil.charts.client.ChartServiceAsync;
import com.williamoneil.charts.client.Charts;
import com.williamoneil.charts.shared.ChartData;
import com.williamoneil.charts.shared.ChartResponse;
import com.williamoneil.charts.shared.PeriodicityType;

/**
 * @author gudipati
 *
 */
public class ChartController {

	private ChartController(){
	}
	
	private static final ChartController _instance = new ChartController();
	public static final ChartController getInstance(){
		return _instance;
	}
	
	public void loadSymbol(final String symbol, final PeriodicityType periodicity) {
		// first load benchmark symbol if it is needed
		loadBenchmarkSymbol(periodicity);
		
		//next get data from server for the symbol
		final ChartServiceAsync chartService = Charts.getChartMainPanel().getChartService();
		chartService.getChartData(symbol, periodicity, 0, (Date)null, new AsyncCallback<ChartResponse>() {
			@Override
			public void onFailure(Throwable caught) {
				Charts.showError(caught);
			}
			@Override
			public void onSuccess(ChartResponse result) {
				try {
					if(result != null && result.getHeader() != null) {
						if(result.getHeader().isError()) {
							Charts.showError(result.getHeader().getErrorMessage());
						} else {
							final ChartData benchmarkChartData = getBenchmarkChart(periodicity);
							Charts.getChartMainPanel().handleChartDataResponse(result.getChartData(), benchmarkChartData);
						}
					}
				}catch(Exception ex) {
					Charts.showError(ex);
				}
			}
		});
	}
	
	private static final Map<Integer, ChartData> benchmarkPeriodicityMap = new HashMap<Integer, ChartData>();
	private static final String benchmarkSymbol = "^GSPC";
	public void loadBenchmarkSymbol(final PeriodicityType periodicity) {
		if(benchmarkPeriodicityMap.containsKey(periodicity)){
			return;
		}
		
		final ChartServiceAsync chartService = Charts.getChartMainPanel().getChartService();
		chartService.getChartData(benchmarkSymbol, periodicity, 0, (Date)null, new AsyncCallback<ChartResponse>() {
			@Override
			public void onFailure(Throwable caught) {
				Charts.showError("Benchmark symbol data not loaded");
			}
			@Override
			public void onSuccess(ChartResponse result) {
				try {
					if(result != null && result.getHeader() != null) {
						if(result.getHeader().isError()) {
							Charts.showError(result.getHeader().getErrorMessage());
						} else {
							benchmarkPeriodicityMap.put(periodicity.ordinal(), result.getChartData());
						}
					}
				}catch(Exception ex) {
					Charts.showError("Error loadiing benchmark symbol data.");
				}
			}
		});
	}
	
	// gets from cache only
	public ChartData getBenchmarkChart(final PeriodicityType periodicity) {
		return benchmarkPeriodicityMap.get(periodicity.ordinal());
	}
}
